<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="点位名称" prop="nodeName">
        <el-input
          v-model="queryParams.nodeName"
          placeholder="请输入点位名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属区域" prop="regionId">
        <el-select v-model="queryParams.regionId" placeholder="请选择所属区域" clearable style="width: 240px">
          <el-option
            v-for="region in regionOptions"
            :key="region.regionId"
            :label="region.regionName"
            :value="region.regionId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属合作商" prop="partnerId">
        <el-select v-model="queryParams.partnerId" placeholder="请选择所属合作商" clearable style="width: 240px">
          <el-option
            v-for="partner in partnerOptions"
            :key="partner.partnerId"
            :label="partner.partnerName"
            :value="partner.partnerId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商圈类型" prop="businessType">
        <el-select v-model="queryParams.businessType" placeholder="请选择商圈类型" clearable style="width: 240px">
          <el-option
            v-for="dict in dict.type.business_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:node:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:node:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:node:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:node:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="nodeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="点位ID" align="center" prop="id" v-if="columns[0].visible" />
      <el-table-column label="点位名称" align="center" prop="nodeName" :show-overflow-tooltip="true" v-if="columns[1].visible" />
      <el-table-column label="详细地址" align="center" prop="address" :show-overflow-tooltip="true" v-if="columns[2].visible" />
      <el-table-column label="商圈类型" align="center" prop="businessType" v-if="columns[3].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.business_type" :value="scope.row.businessType"/>
        </template>
      </el-table-column>
      <el-table-column label="所属区域" align="center" prop="regionName" :show-overflow-tooltip="true" v-if="columns[4].visible" />
      <el-table-column label="所属合作商" align="center" prop="partnerName" :show-overflow-tooltip="true" v-if="columns[5].visible" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" v-if="columns[6].visible">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:node:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:node:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改点位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="点位名称" prop="nodeName">
          <el-input v-model="form.nodeName" placeholder="请输入点位名称" />
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="商圈类型" prop="businessType">
          <el-select v-model="form.businessType" placeholder="请选择商圈类型" style="width: 100%">
            <el-option
              v-for="dict in dict.type.business_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="所属区域" prop="regionId">
          <el-select v-model="form.regionId" placeholder="请选择所属区域" style="width: 100%">
            <el-option
              v-for="region in regionOptions"
              :key="region.regionId"
              :label="region.regionName"
              :value="region.regionId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="所属合作商" prop="partnerId">
          <el-select v-model="form.partnerId" placeholder="请选择所属合作商" style="width: 100%">
            <el-option
              v-for="partner in partnerOptions"
              :key="partner.partnerId"
              :label="partner.partnerName"
              :value="partner.partnerId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNode, getNode, delNode, addNode, updateNode, exportNode } from "@/api/system/node";
import { listRegion } from "@/api/system/region";
import { listPartner } from "@/api/system/partner";

export default {
  name: "Node",
  dicts: ['business_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 点位表格数据
      nodeList: [],
      // 区域下拉选项
      regionOptions: [],
      // 合作商下拉选项
      partnerOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nodeName: undefined,
        regionId: undefined,
        partnerId: undefined,
        businessType: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        nodeName: [
          { required: true, message: "点位名称不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "详细地址不能为空", trigger: "blur" }
        ],
        businessType: [
          { required: true, message: "商圈类型不能为空", trigger: "change" }
        ],
        regionId: [
          { required: true, message: "所属区域不能为空", trigger: "change" }
        ],
        partnerId: [
          { required: true, message: "所属合作商不能为空", trigger: "change" }
        ]
      },
      // 列信息
      columns: [
        { key: 0, label: "点位ID", visible: false },
        { key: 1, label: "点位名称", visible: true },
        { key: 2, label: "详细地址", visible: true },
        { key: 3, label: "商圈类型", visible: true },
        { key: 4, label: "所属区域", visible: true },
        { key: 5, label: "所属合作商", visible: true },
        { key: 6, label: "创建时间", visible: true }
      ]
    };
  },
  created() {
    this.getList();
    this.getRegionOptions();
    this.getPartnerOptions();
  },
  methods: {
    /** 查询点位列表 */
    getList() {
      this.loading = true;
      listNode(this.queryParams).then(response => {
        this.nodeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 获取区域下拉选项 */
    getRegionOptions() {
      listRegion().then(response => {
        this.regionOptions = response.rows;
      });
    },
    /** 获取合作商下拉选项 */
    getPartnerOptions() {
      listPartner().then(response => {
        this.partnerOptions = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        nodeName: undefined,
        address: undefined,
        businessType: undefined,
        regionId: undefined,
        partnerId: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加点位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const nodeId = row.id || this.ids[0];
      getNode(nodeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改点位";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateNode(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNode(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const nodeIds = row.id || this.ids;
      this.$modal.confirm('是否确认删除点位编号为"' + nodeIds + '"的数据项?').then(function() {
        return delNode(nodeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有点位数据项?').then(() => {
        this.exportLoading = true;
        return exportNode(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script> 